package com.msconfig.mvp.view.activity.base;

/*
 * author: caojunhao
 * date  : 2020/4/28
 * desc  :
 */

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    private Unbinder unBinder;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(this.getLayoutId(), container, false);
        unBinder = ButterKnife.bind(this, view);

        initView(view);
        initListener(view);
        initData();

        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        return view;
    }



    @Override
    public void onDestroyView() {
        if (unBinder != null) {
            unBinder.unbind();
        }
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        super.onDestroyView();
    }

    /**
     * 初始化视图
     *
     */
    protected abstract int getLayoutId();

    protected abstract void initView(View view);

    protected abstract void initListener(View view);

    protected abstract void initData();

    protected abstract void showToast(int resId);


}
